﻿Imports System.Data.SqlClient
Public Class LoaiTinDAO
    Public Function GetItemID(ByVal ten As String) As LoaiTinDTO
        Dim loaitin As New LoaiTinDTO
        Dim cnn As New SqlConnection
        Dim dataProvider As New DataProvider
        Dim da As SqlDataAdapter
        Dim strCon As String = "select *from LOAITIN where TenLoai = N'" + ten + "'"
        cnn = dataProvider.ConnectionData()
        da = New SqlDataAdapter(strCon, cnn)
        Dim data As New DataTable
        da.Fill(data)
        Try
            loaitin.MaLoaiTin = Convert.ToInt32(data.Rows(0)("MaLoaiTin").ToString())
            loaitin.TenLoai = data.Rows(0)("TenLoai").ToString()
            loaitin.DonGia = Convert.ToInt32(data.Rows(0)("DonGia").ToString())
        Catch ex As Exception

        End Try

        Return loaitin
    End Function
    Public Function GetAll() As DataTable
        Dim cnn As New SqlConnection
        Dim dataProvider As New DataProvider
        Dim da As SqlDataAdapter
        Dim strCon As String = "select *from LOAITIN"
        cnn = dataProvider.ConnectionData()
        da = New SqlDataAdapter(strCon, cnn)
        Dim data As New DataTable
        Try
            da.Fill(data)
        Catch ex As Exception

        End Try
        Return data
    End Function
    Public Sub Insert(ByVal loai As LoaiTinDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_InsertLOAITIN @TenLoai,@DonGia,@MaLoaiTin"
            Dim para As SqlParameter = cmd.Parameters.Add("@TenLoai", SqlDbType.NVarChar)
            para.Value = loai.TenLoai
            para = cmd.Parameters.Add("@DonGia", SqlDbType.Int)
            para.Value = loai.DonGia
            para = cmd.Parameters.Add("@MaLoaiTin", SqlDbType.Int)
            para.Value = loai.MaLoaiTin
            cmd.ExecuteNonQuery()
            MessageBox.Show("Loại tin đã được tạo thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub Update(ByVal loai As LoaiTinDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_UpdateLOAITIN @TenLoai,@DonGia,@MaLoaiTin"
            Dim para As SqlParameter = cmd.Parameters.Add("@TenLoai", SqlDbType.NVarChar)
            para.Value = loai.TenLoai
            para = cmd.Parameters.Add("@DonGia", SqlDbType.Int)
            para.Value = loai.DonGia
            para = cmd.Parameters.Add("@MaLoaiTin", SqlDbType.Int)
            para.Value = loai.MaLoaiTin
            cmd.ExecuteNonQuery()
            MessageBox.Show("Loại tin đã được cập nhật thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub Delete(ByVal loai As LoaiTinDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_DeleteLOAITIN @MaLoaiTin"
            Dim para As SqlParameter = cmd.Parameters.Add("@MaLoaiTin", SqlDbType.Int)
            para.Value = loai.MaLoaiTin
            cmd.ExecuteNonQuery()
            MessageBox.Show("Loại tin đã xóa tạo thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
